import java.util.Deque;
import java.util.LinkedList;

public class num32 {
    public int longestValidParentheses(String s) {
        int count = 0;
        Deque<Integer> stack = new LinkedList<>();
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == '(') {
                stack.push(i);
            }else {
                stack.pop();
                if(stack.isEmpty()) {
                    stack.push(i);
                }else {
                    count = Math.max(count,i - stack.peek());
                }
            }
        }
        return count;
    }
}
